<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Session;

class Notice extends  Base{
	
  public  function release(){
  	
	return $this->fetch();
  }
	
  public function upload(){
  	$files=$this->request->file();
	 $file_arr = [];
	  if($files){
		  foreach($files as $file){
			if($file){
				$info=$file->move(ROOT_PATH . 'public/static' . DS . 'img');
				if($info){
					$filename=$info->getSaveName();
					//var_dump($filename);exit;
				}else{
					$filename="";
				}
			}else{
				$filename="";
			}
				$arr = [
					'code'   => 0,
					'msg'    => '图片上传成功',
					'data'   => array(
						'src'  => '\static\img\\'.$filename,
					),
				];
	  
		  }
  }
     return json($arr);
}
	
	//系统消息添加
   public function release_add(){
	$arr = Request::instance()->post();
	$admin_id= Session::get('uid');
	$arr['add_time']=time();
	$arr['type']=3;
	$arr['admin_id']=$admin_id;
	$res = Db::table('think_system_message')->insert($arr);
	if($res){
		$att['code']=1;
		$att['msg']='添加成功';
	}else{
		$att['code']=0;
		$att['msg']='添加失败';
	}
	return $att;
   }	

  //消息列表
  public function list(){
  	$arr = Db::table('think_system_message')->where('type','EQ',3)->paginate(5,false,['query'=>input()]);
	$this->assign('arr',$arr);
	return $this->fetch();
  }	
	
  public function selectNews(){
  	//$arr = Request::instance()->post();
  	$arr = input();
	$uid =$arr['personnel'];
	$start_time = $arr['start_time'];
	$end_time = $arr['end_time'];
	$start_time =  strtotime($start_time);
	$end_time  = strtotime($end_time);
	if($uid!='请选择'){
		if($start_time && $end_time){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('uid','EQ',$uid)->where('add_time', 'between', [$start_time,$end_time])->paginate(5,false,['query'=>input()]);
		}else if($start_time =='' && $end_time!=''){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('uid','EQ',$uid)->where('add_time', '<', $end_time)->paginate(5,false,['query'=>input()]);
		}else if($end_time=='' && $start_time!=''){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('uid','EQ',$uid)->where('add_time', '>', $start_time)->paginate(5,false,['query'=>input()]);
		}else if($end_time=='' && $start_time==''){
		 $arr = Db::table('think_system_message')->where('type','EQ',3)->where('uid','EQ',$uid)->paginate(5,false,['query'=>input()]);
		}
	}else{
		if($start_time && $end_time){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('add_time', 'between', [$start_time,$end_time])->paginate(5,false,['query'=>input()]);
		}else if($start_time =='' && $end_time!=''){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('add_time', '<', $end_time)->paginate(5,false,['query'=>input()]);
		}else if($end_time=='' && $start_time!=''){
		$arr = Db::table('think_system_message')->where('type','EQ',3)->where('add_time', '>', $start_time)->paginate(5,false,['query'=>input()]);
		}else if($end_time=='' && $start_time==''){
		 $arr = Db::table('think_system_message')->where('type','EQ',3)->paginate(5,false,['query'=>input()]);
		}
	}
	$this->assign('uid',$uid);
	$this->assign('arr',$arr);
    return $this->fetch('notice/list');
  }	
  
   //删除新闻
   public function deleteNews(){
   	$id = input('id');
	$arr = Db::table('think_system_message')->where('id','EQ',$id)->delete();
	if($arr){
		$data['code']=1;
		$data['msg']='删除成功';
	}else{
		$data['code']=0;
		$data['msg']='系统忙,稍后再试';
	}
	return $data;
   }

}
